package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderListDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.service.OrderListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderListService.class)
@Transactional
public class OrderListServiceImpl implements OrderListService {
    @Autowired
    private OrderListDao orderListDao;

    @Autowired
    private MemberDao memberDao;
    
    //查询分页数据
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取分页查询需要的数据
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //利用PageHelper进行分页查询
        PageHelper.startPage(currentPage, pageSize);
        Page<Map<String,String>> page = orderListDao.findPage(queryString);
        
        long total = page.getTotal();
        List<Map<String,String>> rows = page.getResult();
        return new PageResult(total, rows);
    }

    @Override
    public void addOrder(Order order) {
        orderListDao.addOrder(order);
    }

    @Override
    public void addMember(Member member) {
       memberDao.add(member);
    }

    @Override
    public void addOrderAndMember(Map map, Integer[] setmeal_id) {
        try {
            String name = (String) map.get("name");
            String idCard = (String) map.get("idCard");
            String birthday = (String) map.get("birthday");
            String phoneNumber = (String) map.get("phoneNumber");
            String sex = (String) map.get("sex");
            String orderDate = (String) map.get("orderDate");
            //新建预约对象并赋值
            Date Dateorderdate = new SimpleDateFormat("yyyy-MM-dd").parse(orderDate);
            Order order = new Order();
            order.setOrderDate(Dateorderdate);
            //默认将状态设为未到诊
            order.setOrderStatus(Order.ORDERSTATUS_NO);
            order.setSetmealId(setmeal_id[0]);

            //新建会员对象并赋值
            Date DateBirthday = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
            Member member = new Member();
            member.setName(name);
            member.setSex(sex);
            member.setIdCard(idCard);
            member.setPhoneNumber(phoneNumber);
            member.setBirthday(DateBirthday);

            //先注册会员,返回会员id
            memberDao.add(member);
            Integer id = member.getId();

            System.out.println("用户id是"+id);
            order.setMemberId(id);
            //再新增预约
            orderListDao.addOrder(order);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    
    
    
    @Override
    public void delete(Integer id) {
        orderListDao.delete(id);
    }

    @Override
    public void deleteAllSelected(String[] ids) {
        //遍历之后一个个删除
        for (String id : ids) {
            orderListDao.delete(Integer.parseInt(id));
        }
    }

    @Override
    public Date selectOrderDateById(Integer id) {
        return orderListDao.selectOrderDateById(id);
    }

    @Override
    public void updateOrderStatusById(Integer id) {
        orderListDao.updateOrderStatusById(id);
    }


}
